package testJdk.commonUse.collections.list;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author cyd
 * @version 1.0.0
 * @date 2021/12/9 16:19
 */


public class TestArrayList {

    /**
     * 测试队列的边界
     */
    @Test
    public void test01() {
        ArrayList<Object> list = new ArrayList<>(7);
        // 即使初始化时指定了长度，依然会超标
        System.out.println(list.get(5));
    }

    /**
     * 队列的拼接
     */
    @Test
    public void test02() {
        ArrayList<Integer> list01 = Lists.newArrayList(1, 2, 3, 4);
        ArrayList<Integer> list02 = Lists.newArrayList(4, 5, 6);
        List<Integer> mergedList = Stream.of(list01, list02).flatMap(Collection::stream).collect(Collectors.toList());
        System.out.println(mergedList);

        boolean b = list01.addAll(list02);
        System.out.println(list01);
    }

    /**
     * 队列的拼接2
     */
    @Test
    public void test04() {
        ArrayList<Integer> list01 = Lists.newArrayList(1, 2, 3, 4);
        ArrayList<Integer> list02 = Lists.newArrayList(4, 5, 6);
        List<Integer> mergedList01 = Stream.of(list01, list02).flatMap(Collection::stream).collect(Collectors.toList());

        List<Integer> mergedList02 = Stream.of(null, list02).filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toList());

        System.out.println(mergedList01);
        System.out.println(mergedList02);
    }

    /**
     * 队列拼接成字符串
     */
    @Test
    public void test03() {
        // 将list转化为字符串
        ArrayList<Integer> list = Lists.newArrayList(1, 2, 3);
        System.out.println(list);
        String s = StrUtil.toString(list);
        System.out.println(s);
        String s1 = StrUtil.removeAny(s, "]", "[");
        System.out.println(s1);

        // 将字符串还原为list，有问题的，转化回来后变成 List<String> 了
        ArrayList<String> strings = Lists.newArrayList(s1.split(","));
        System.out.println(strings);

    }

    /**
     * 以Object为泛型的list
     */
    @Test
    public void test05(){
        List<Object> list = Arrays.asList("hhh", 123, true);
        System.out.println(list);
        System.out.println("-------------");
        ArrayList<Object> arrayList = new ArrayList<>();
        arrayList.add("jhh");
        arrayList.add(123);
        arrayList.add(false);
        System.out.println(arrayList);
    }
}
